package com.yc.jpyy.mobile.controller;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/teacherinfo")
public class GetTeacherListInfo extends BaseController{
	@Autowired
	private BizService bizService;
	private static Logger logger = LoggerFactory.getLogger(GetTeacherListInfo.class);	
	@Autowired
	private BizDao bizDao;
	
	/**
	 * 获取教练列表
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getTeacherList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getTeacherList(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {	
			String PageNum = request.getParameter("PageNum");
			String PageSize = request.getParameter("PageSize");
			//区县编号
			String regionNo = request.getParameter("regionNo");
			//驾校ID
			String driverId = request.getParameter("driverId");
			//驾校类型(1,总校；2,分校)
			String driveType = request.getParameter("driveType");
			
			if(regionNo != null && !regionNo.isEmpty()){
				if ((PageNum != null && !PageNum.isEmpty()) || (PageSize != null
						&& !PageSize.isEmpty())) {
					if (PageNum.equals("0") || PageSize.equals("0")) {
						response.setErrorcode(1);
						response.setMessage("页码或页数不能为0...");
						return response.toJSON();
					}
				}
				//获取请求体中的参数（除了这些不为空的确定参数，还有可能有其他参数）
				Map<String, Object> paramsMap = getParamMap(request);
				paramsMap.put("imgUrl", Config.getImageAddress());
				//根据区县编号找到对应驾校，然后再获取这些驾校下的教练列表(只获取分校下的教练，因为教练最后分划到分校，档案在总校)
				response = bizService.getTeacherListByRegion(paramsMap,response);
			}else if((driverId != null && !driverId.isEmpty()) 
				&& (driveType != null && !driveType.isEmpty())){
				/*Map<String, Object> paramsMap = new HashMap<>();
				paramsMap.put("driverId", driverId);
				paramsMap.put("driveType", driveType);*/
				//获取请求体中的参数（除了这些不为空的确定参数，还有可能有其他参数）
				Map<String, Object> paramsMap = getParamMap(request);
				paramsMap.put("imgUrl", Config.getImageAddress());
				//根据驾校找教练
				List<Map<String, Object>> map = bizService.getTeacherListBySchoolID(paramsMap);
				if (map != null && map.size() > 0) {
					response.setData(map);
				} else {
					response.setErrorcode(1);
					response.setMessage("无教练信息...");
				}
			}else{
				response.setErrorcode(1);
				response.setMessage("必填参数为空！");
			}
		} catch (Exception e) {
			logger.info("getTeacherList:"+ e.toString());
			response.setReturnMsg(1,"获取教练列表信息失败！");
		} finally {
			return response.toJSON();
		}
	}
	/*
	 * 根据教练ID获取教练详情
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getTeacherDetailById", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getTeacherDetailById(HttpServletRequest request) throws UnsupportedEncodingException{
		
		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {	
			//教练ID
			String teacherId = request.getParameter("teacherId");
			if(teacherId != null && !teacherId.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("teacherId", teacherId);
				paramsMap.put("imgUrl", Config.getImageAddress());
				Map<String,Object> teacherMap = bizDao.getTeacherDetailById(paramsMap);
				if(teacherMap != null && teacherMap.size() > 0){
					response.setErrorcode(0);
					response.setData(teacherMap);
				}else{
					response.setErrorcode(1);
					response.setMessage("未查询到该教练的具体详情");
				}
			}else{
				response.setErrorcode(1);
				response.setMessage("必填参数为空！");
			}	
		} catch (Exception e) {
			logger.info("getTeacherDetailById:"+ e.toString());
			response.setReturnMsg(1,"获取教练信息失败！");
		} finally {
			return response.toJSON();
		}
	}
}
